# -*- coding: utf-8 -*-
# @Time    : 2025/2/26 12:24
# @Author  : cx
# @File    : ls.py
# @Software: PyCharm
# -*- coding: utf-8 -*-
# @Time    : 2025/2/26 10:15
# @Author  : cx
# @File    : main.py
# @Software: PyCharm

from urllib import request
from urllib import parse  # 编码要用到
from lxml import etree  # 解析HTML代码
import requests
from pymysql import connect
from requests.exceptions import RequestException  # 抛出网络请求异常
import re  # 正则表达式
from http.cookiejar import CookieJar  # Cookie专用
import time
import os
from GongJuKu import MYSQL_CZ
# 随机字符需要的2个库
import random
import string
#字符编码处理
import chardet


"""
小说抓取
"""

class XiaoShuo:
    __daMing = "https://sk.3qdu.com/xiaoshuo/629142/"
    #__referer = None

    def __init__(self):
        self.__referer = self.__daMing

    def qing_qiu_tou(self, referer): # 来路
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36",
            "referer": referer
        }
        return headers

    def get_mu_lu(self, url):
        headers = self.qing_qiu_tou(self.__referer)
        res2 = requests.get(url, headers=headers)

        # 获取网页数据并将数据解码为html格式
        #htmltext = etree.HTML(res2.content.decode())
        # htmltext = etree.HTML(res2.content.decode("gbk"))
        # 检测编码
        encoding = chardet.detect(res2.content)['encoding']
        print(f"检测到编码: {encoding}")  # 调试输出编码类型
        # 解码并解析
        try:
            htmltext = etree.HTML(res2.content.decode(encoding))
        except UnicodeDecodeError:
            # 回退到常见中文编码
            try:
                htmltext = etree.HTML(res2.content.decode("gbk"))
            except Exception as e:
                print(f"解码失败: {e}")
                exit()

        #res2.encoding = "gbk"  # 手动指定编码（如果已知）
        #htmltext = etree.HTML(res2.text)  # 自动解码为字符串
        print(htmltext)
        # 假设 htmltext 是你的 Element 对象
        html_str = etree.tostring(
            htmltext,  # Element 对象
            encoding="utf-8",  # 指定编码（与原网页一致）
            pretty_print=True,  # 格式化输出（自动缩进）
            method="html"  # 输出为 HTML（默认）
        ).decode("utf-8")  # 将字节流转为字符串

        print(html_str)


"""
58同城紫荆商业广场二手房(35-48平)信息抓取
"""
class TongCheng58ErShouFangL:
    __referer = "https://cq.58.com/ershoufang/"
    __Cookie = 'f=n; commontopbar_new_city_info=37%7C%E9%87%8D%E5%BA%86%7Ccq; commontopbar_ipcity=cq%7C%E9%87%8D%E5%BA%86%7C0; userid360_xml=82318F1B293F6A835170373D0F5EB460; time_create=1728396976909; xxzlclientid=be25e1c8-de10-49e4-9a88-1725317784074; xxzlxxid=pfmxI0491dL43aD1MkKjENeRv+53eZZSQ2GHnCgzYRr5ornLg8wPxXTWOjfh9Wp27mYd; id58=CrIekmbdsYh2DbkLhBh7Ag==; 58tj_uuid=d23c711f-9d92-4159-8a54-3656132612a2; new_uv=1; utm_source=; spm=; init_refer=https%253A%252F%252Fcq.58.com%252Fchuzu%252Fpn2%252F%253Fkey%253D%2525E7%2525B4%2525AB%2525E8%25258D%252586%2525E5%252595%252586%2525E4%2525B8%25259A%2525E5%2525B9%2525BF%2525E5%25259C%2525BA%2526PGTID%253D0d3090a7-0002-5598-c0b4-2740f74ad575%2526ClickID%253D41; als=0; 58home=cq; f=n; new_session=0; commontopbar_new_city_info=37%7C%E9%87%8D%E5%BA%86%7Ccq; city=cq; commontopbar_ipcity=cq%7C%E9%87%8D%E5%BA%86%7C0; wmda_uuid=286ebb5e17b507c57a5c5c91b9709725; wmda_new_uuid=1; wmda_session_id_10104579731767=1725807262286-f1059b77-e7fc-a65e; wmda_visited_projects=%3B10104579731767; www58com="UserID=82211940993032&UserName=a13996070559"; 58cooper="userid=82211940993032&username=a13996070559"; 58uname=a13996070559; passportAccount="atype=0&bstate=0"; PPU=UID=82211940993032&UN=a13996070559&TT=ca13a4507588bcd2801c20a0a562b729&PBODY=dPN0rJaOvygWKGiKZPpMzP624Utxac0Jw5pQnmcAaMRLyjT6DwFKdUKFHqANL3plhuB93DzCYFk_MqZSID_y5G_-gSHb6gyuR5dMM73ti-3SdT4ApUxHcbbGbvWzrhBnw7DuFjOQEk4X8Ftjm34I396qvWkRBXJPKYhfbdPQ_AU&VER=1&CUID=6-inOdquafA4zA1Y55OjqQ; xxzlbbid=pfmbM3wxMDI5MnwxLjEwLjB8MTcyNTgwNzM5MDE4MjczODA1NHw4SWJhQUEyeTgrMU91TGkwVDNCRkQ2bkt0WEFOeFA2dVJjQXBhN05LVE5vPXw2YmNhODAwMGVmYjAwNjBiYmE2ZWVjOWQ1Yjc0ZTVhYV8xNzI1ODA3Mzg3NjQ3X2RkMTEzOGEzZTdhMjRjM2NiODQ0NmM2NzA1ZDgwMzFjXzIxMDI0OTk1OTd8NjMwNjRjODU3NWU4ZGE4MTNlNzg0ZDZiOTYwZjQ5MTlfMTcyNTgwNzM4ODQ0M18yNTU=; xxzl_cid=cabbb7033783405abb725d7f0070eb59; xxzl_deviceid=zUxKOFHzq15Nh1oQTRmSBsQ7Yuu949XJNwD4j2nllz9NGBSLpbOz3hii4qumMhKh'
    # https://cq.58.com/chuzu/?newSearch=1&key=紫荆商业广场
    syUrl = 'https://cq.58.com/ershoufang/?q=%E7%B4%AB%E8%8D%86%E5%95%86%E4%B8%9A%E5%B9%BF%E5%9C%BA'

    # 组合抓取网址
    def __init__(self):
        # 获取结果个数,循环次数
        self.cis = 0
        self.sqlm = MYSQL_CZ()

    def qing_qiu_tou(self, referer, Cookie):
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
            "referer": referer,
            "Cookie": Cookie
        }
        return headers

    def get_mu_lu(self, url):
        headers = self.qing_qiu_tou(self.__referer, self.__Cookie)
        res2 = requests.get(url, headers=headers)
        # 获取网页数据并将数据解码为html格式
        htmltext = etree.HTML(res2.content.decode())

        lou_pan_di_zhi = "紫荆商业广场"
        zui_da_mian_ji = 48
        zui_xiao_mian_ji = 35

        # 用xpath提取数据,它返回的结果是个列表集(数组)
        dang_yue_jun_jia = htmltext.xpath(
            "//div[@data-action='esf_List_comm_exp']//p[@class='community-info-detail-price-money money-rise']/em/text()")  # <li id="pager_wrap">
        print(dang_yue_jun_jia[0])
        sf_di_zhi = htmltext.xpath("//div[@class='community-info-detail-title']/h3[1]/text()")
        xiao_qu = ''.join(sf_di_zhi[0].split())
        current_timestamp = time.time()
        shiJian = self.sqlm.cx_xiao_qu_shi_jian_cuo()
        if shiJian:
            if current_timestamp > (shiJian + 86400):
                panDuan = self.sqlm.cx_xiao_qu_jj(shiJian, dang_yue_jun_jia[0])
                if panDuan != xiao_qu:
                    self.sqlm.zj_xiao_qu_jun_jia(current_timestamp, dang_yue_jun_jia[0], xiao_qu)
        else:
            self.sqlm.zj_xiao_qu_jun_jia(current_timestamp, dang_yue_jun_jia[0], xiao_qu)
        trs = htmltext.xpath("//div[@tongji_tag='fcpc_ersflist_gzcount']")
        for li in trs:
            # 楼盘地址
            sf_di_zhi = li.xpath(".//div[@class='property-content-info property-content-info-comm']/p[1]/text()")
            # 房屋面积
            sf_mianJi = li.xpath(".//div[@class='property-content-info']/p[2]/text()")
            if sf_di_zhi[0] == lou_pan_di_zhi:
                # 去除字符串中的无效字符" ",就是引号中的字符
                full_text = ''.join(sf_mianJi[0].split())
                # 使用正则表达式匹配“室”字后面的整数或浮点数
                match = re.search(r'(\d+\.\d+|\d+)', full_text)
                # 如果找到匹配项，则返回匹配的数字
                if match:
                    # group(1) 获取第一个括号中的匹配项
                    number_str = match.group(1)
                    # 如果结果中有"."
                    if '.' in number_str:
                        # 转换为浮点数
                        sf_mianJi = float(number_str)
                    else:
                        # 转换为整数
                        sf_mianJi = int(number_str)
                else:
                    continue  # 跳过当前迭代
                if sf_mianJi > zui_da_mian_ji or sf_mianJi < zui_xiao_mian_ji:
                    continue  # 跳过当前迭代
            else:
                continue
            # 信息标题
            sf_biaoTi = li.xpath(".//div[@class='property-content-title']/h3/@title")
            # 房屋卖价
            sf_mai_jia = li.xpath(".//div[@class='property-price']//span[1]/text()")
            # 建造年代
            sf_nian_dai_1 = li.xpath(".//div[@class='property-content-info']/p[5]/text()")

            # 平方均价
            sf_jun_jia = li.xpath(".//div[@class='property-price']/p[2]/text()")

            panDuan = self.sqlm.cx_er_shou_fang_bt_sj(sf_biaoTi[0],sf_mai_jia[0])
            # print("pan : ",panDuan)
            # 如果返回不等于None,就说明有相同的数据,就跳过这一条
            # 生成当前时间戳
            current_timestamp = time.time()
            # 如果都一样
            if panDuan:
                # 提取结果中列表panDuan[2]的最大值,panDuan[2]=时间戳字段
                max_tuple = max(panDuan, key=lambda x: x[2])
                riqi = max_tuple[2]
                # 24小时 =86400m
                # 如果数据库的时间距离当前有没有5天
                if current_timestamp < (riqi + 86400 * 10):
                    continue  # 跳过当前迭代

            match = re.search(r'(\d+\.\d+|\d+)', sf_jun_jia[0])
            if match:
                number_str = match.group(1)  # group(1) 获取第一个括号中的匹配项
                if '.' in number_str:
                    sf_jun_jia = float(number_str)
                else:
                    sf_jun_jia = int(number_str)
            else:
                continue

            full_text = ''.join(sf_nian_dai_1)
            # print(full_text)
            # 使用正则表达式匹配整数
            match = re.search(r'(\d+)', full_text)
            if match:
                number_str = match.group(1)  # group(1) 获取第一个括号中的匹配项
                # 建造年代
                sf_nian_dai = int(number_str)
            else:
                # 要是信息中没有
                sf_nian_dai = None


            self.sqlm.zj_er_shou_fang_tiao_mu(sf_mianJi,sf_mai_jia[0],sf_jun_jia,"",
                                              sf_biaoTi[0],sf_di_zhi[0],sf_nian_dai,current_timestamp)

            print(sf_nian_dai)
            print(sf_mianJi)
            print(sf_mai_jia[0])
            print(sf_biaoTi[0])

            print(sf_jun_jia)
            print(sf_di_zhi[0])
            print("\r")
            self.cis += 1
        # print(trs)

        print(self.cis)

    def run(self):
        print("======= 开始抓取 紫荆商业广场43平米左右 二手房信息 =========")
        self.get_shou_ye(self.syUrl)
        print(f"======= 抓取完成,共获取 {self.cis}条 数据 ========")
"""
58同城紫荆商业广场租房(35-48平)信息抓取
"""
class TongCheng58ZuFangL:
    # __referer = "https://cq.58.com/?from=pc_topbar_home&PGTID=0d200001-0002-5459-6692-587fedc02738&ClickID=1"# https://cq.58.com/chuzu/
    __referer = "https://cq.58.com/chuzu/?PGTID=0d100000-0002-539b-f661-3773a75516d9&ClickID=5"
    __Cookie = 'f=n; commontopbar_new_city_info=37%7C%E9%87%8D%E5%BA%86%7Ccq; commontopbar_ipcity=cq%7C%E9%87%8D%E5%BA%86%7C0; userid360_xml=82318F1B293F6A835170373D0F5EB460; time_create=1728396976909; xxzlclientid=be25e1c8-de10-49e4-9a88-1725317784074; xxzlxxid=pfmxI0491dL43aD1MkKjENeRv+53eZZSQ2GHnCgzYRr5ornLg8wPxXTWOjfh9Wp27mYd; id58=CrIekmbdsYh2DbkLhBh7Ag==; 58tj_uuid=d23c711f-9d92-4159-8a54-3656132612a2; new_uv=1; utm_source=; spm=; init_refer=https%253A%252F%252Fcq.58.com%252Fchuzu%252Fpn2%252F%253Fkey%253D%2525E7%2525B4%2525AB%2525E8%25258D%252586%2525E5%252595%252586%2525E4%2525B8%25259A%2525E5%2525B9%2525BF%2525E5%25259C%2525BA%2526PGTID%253D0d3090a7-0002-5598-c0b4-2740f74ad575%2526ClickID%253D41; als=0; 58home=cq; f=n; new_session=0; commontopbar_new_city_info=37%7C%E9%87%8D%E5%BA%86%7Ccq; city=cq; commontopbar_ipcity=cq%7C%E9%87%8D%E5%BA%86%7C0; wmda_uuid=286ebb5e17b507c57a5c5c91b9709725; wmda_new_uuid=1; wmda_session_id_10104579731767=1725807262286-f1059b77-e7fc-a65e; wmda_visited_projects=%3B10104579731767; www58com="UserID=82211940993032&UserName=a13996070559"; 58cooper="userid=82211940993032&username=a13996070559"; 58uname=a13996070559; passportAccount="atype=0&bstate=0"; PPU=UID=82211940993032&UN=a13996070559&TT=ca13a4507588bcd2801c20a0a562b729&PBODY=dPN0rJaOvygWKGiKZPpMzP624Utxac0Jw5pQnmcAaMRLyjT6DwFKdUKFHqANL3plhuB93DzCYFk_MqZSID_y5G_-gSHb6gyuR5dMM73ti-3SdT4ApUxHcbbGbvWzrhBnw7DuFjOQEk4X8Ftjm34I396qvWkRBXJPKYhfbdPQ_AU&VER=1&CUID=6-inOdquafA4zA1Y55OjqQ; xxzlbbid=pfmbM3wxMDI5MnwxLjEwLjB8MTcyNTgwNzM5MDE4MjczODA1NHw4SWJhQUEyeTgrMU91TGkwVDNCRkQ2bkt0WEFOeFA2dVJjQXBhN05LVE5vPXw2YmNhODAwMGVmYjAwNjBiYmE2ZWVjOWQ1Yjc0ZTVhYV8xNzI1ODA3Mzg3NjQ3X2RkMTEzOGEzZTdhMjRjM2NiODQ0NmM2NzA1ZDgwMzFjXzIxMDI0OTk1OTd8NjMwNjRjODU3NWU4ZGE4MTNlNzg0ZDZiOTYwZjQ5MTlfMTcyNTgwNzM4ODQ0M18yNTU=; xxzl_cid=cabbb7033783405abb725d7f0070eb59; xxzl_deviceid=zUxKOFHzq15Nh1oQTRmSBsQ7Yuu949XJNwD4j2nllz9NGBSLpbOz3hii4qumMhKh'
    # https://cq.58.com/chuzu/?newSearch=1&key=紫荆商业广场
    syUrl = 'https://cq.58.com/chuzu/?newSearch=1&key=%E7%B4%AB%E8%8D%86%E5%95%86%E4%B8%9A%E5%B9%BF%E5%9C%BA'
    muBiaoUrl = 'https://cq.58.com/chuzu/pn{}/?key=%E7%B4%AB%E8%8D%86%E5%95%86%E4%B8%9A%E5%B9%BF%E5%9C%BA'

    # 组合抓取网址
    def __init__(self):
        # 获取结果个数,循环次数
        self.cis = 0
        self.url_list = []
        self.uri_list = []
        self.sqlm = MYSQL_CZ()

        for i in range(3):
            # i 自加1
            i += 1
            # 填充i代表的字符到目标字符串
            url = self.muBiaoUrl.format(i)
            self.uri_list.append(url)

    def zu_he_url(self, yeSu):
        for i in range(yeSu):
            i += 1
            if i < 2:
                continue  # 跳过当前迭代
            urlzh = self.muBiaoUrl.format(i)
            self.url_list.append(urlzh)

    # 获得随机以字母开头的20位字符串
    def tp_ming(self):
        # 随机选择一个字母作为首字符
        first_char = random.choice(string.ascii_letters)
        # 生成剩余19位的随机字符串，包含字母和数字
        remaining_chars = ''.join(random.choices(string.ascii_letters + string.digits, k=19))
        # 将首字符和剩余字符组合成20位的字符串
        random_string = first_char + remaining_chars
        return random_string

    # 组合请求头
    def qing_qiu_tou(self, referer, Cookie):
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
            "referer": referer,
            "Cookie": Cookie
        }
        return headers

    def get_shou_ye(self, url):
        headers = self.qing_qiu_tou(self.__referer, self.__Cookie)
        res2 = requests.get(url, headers=headers)
        # 获取网页数据并将数据解码为html格式
        hhtml = etree.HTML(res2.content.decode())
        # 用xpath提取数据,它返回的结果是个列表集(数组)
        trs = hhtml.xpath("//li[@class='house-cell realverify']")
        # print(trs)
        # for循环
        for li in trs:

            mianJi = li.xpath(".//p[@class='room']/text()")
            # print(mianJi[0])

            # 拼接所有文本节点，并使用正则表达式提取数字
            full_text = ''.join(mianJi[0].split())
            # print(full_text)
            # 使用正则表达式匹配“室”字后面的整数或浮点数
            match = re.search(r'室(\d+\.\d+|\d+)', full_text)
            # 如果找到匹配项，则返回匹配的数字
            pf = 0
            if match:
                number_str = match.group(1)  # group(1) 获取第一个括号中的匹配项
                if '.' in number_str:
                    pf = float(number_str)
                else:
                    pf = int(number_str)

            if pf <= 46 and pf >= 38:

                gs = li.xpath(".//span [@class='jjr_par_dp']/@title")  # 发布房源的经纪公司
                jg = li.xpath(".//b/text()")  # 房租价格
                mz = li.xpath(".//a[@class='strongbox']/text()")  # 发布的标题
                mz1 = ''.join(mz[0].split())  # 保留有效字符
                faBuRiQi = li.xpath(".//div[@class='send-time']/text()")  # 发布时间
                faBuRiQi1 = ''.join(faBuRiQi[0].split())  # 保留有效字符
                tuPanDiZhi = li.xpath(".//img/@lazy_src")
                lianJie = li.xpath(".//div[@class= 'des']/h2/a/@href")
                try:
                    jgint = int(jg[0])
                except ValueError as e:
                    jgint = 10

                # 获取图片名
                tp_ming_zi = ""
                match = re.search(r'/anjuke_58/([0-9a-z]{32})\?', tuPanDiZhi[0])
                if match:
                    tp_ming_zi = match.group(1) + ".png"
                else:
                    tp_ming_zi = self.tp_ming() + ".png"
                # 生成当前时间戳
                current_timestamp = time.time()
                # 先查询数据库有没有同名图片,然对比价格是否一样
                pd = self.sqlm.cha_xun_tp_jg(tp_ming_zi, jgint)
                # 如果都一样
                if pd:
                    # 提取结果中列表pd[2]的最大值
                    max_tuple = max(pd, key=lambda x: x[2])
                    riqi = max_tuple[2]
                    # 24小时 =86400m
                    # 如果数据库的时间距离当前有没有5天
                    if current_timestamp < (riqi + 86400 * 5):
                        continue  # 跳过当前迭代

                # 暂停300毫秒,下载并保存图片
                time.sleep(0.3)
                try:
                    tU_pian = requests.get(tuPanDiZhi[0], headers=headers)
                    tU_pian.raise_for_status()  # 如果响应状态码指示错误，则抛出HTTPError异常
                    tppath = ".\\tp\\" + tp_ming_zi
                    with open(tppath, "wb") as f:
                        f.write(tU_pian.content)
                except RequestException as e:
                    # 处理请求异常
                    print(f"请求图片失败：{e}")
                    raise
                except IOError as e:
                    # 处理文件I/O异常
                    print(f"保存图片失败：{e}")
                    raise
                except Exception as e:
                    # 处理其他可能的异常
                    print(f"发生未知错误：{e}")
                    raise

                print(pf)
                print(jgint)
                print(mz1)
                print(gs[0])
                print(faBuRiQi1)
                print(tuPanDiZhi[0])
                print(lianJie[0])

                # 将数据保存数据库中
                self.sqlm.zj_zu_fang_tiao_mu(tuPanDiZhi[0], tp_ming_zi, pf, gs[0], mz1, jgint, faBuRiQi1, lianJie[0],
                                           current_timestamp)

                print("\r")
                self.cis = self.cis + 1
                # time.sleep(0.3)
        wy: int = 0
        # 用xpath提取数据,它返回的结果是个列表集(数组)
        cs = hhtml.xpath("//li[@id='pager_wrap']//a")  # <li id="pager_wrap">
        # print(trs)
        # for循环
        for li in cs:
            # gs = li.xpath(".//a/@href")
            # print(li)
            wy = wy + 1
        self.__referer = url
        print(self.cis)
        return wy

    def get_wang_ye(self, url):
        headers = self.qing_qiu_tou(self.__referer, self.__Cookie)
        # 使用保存的cookie访问会员页面    https://cq.58.com/
        # url = 'https://cq.58.com/chuzu/?newSearch=1&key=紫荆商业广场'
        res2 = requests.get(url, headers=headers)

        # 获取网页数据并将数据解码为html格式
        hhtml = etree.HTML(res2.content.decode())
        # 获取结果个数,循环次数
        cis: int = 0
        # 用xpath提取数据,它返回的结果是个列表集(数组)
        trs = hhtml.xpath("//li[@class='house-cell realverify']")
        # print(trs)
        # for循环
        for li in trs:

            mianJi = li.xpath(".//p[@class='room']/text()")
            # print(mianJi[0])

            # 拼接所有文本节点，并使用正则表达式提取数字
            full_text = ''.join(mianJi[0].split())
            # print(full_text)
            # 使用正则表达式匹配“室”字后面的整数或浮点数
            match = re.search(r'室(\d+\.\d+|\d+)', full_text)
            # 如果找到匹配项，则返回匹配的数字
            pf = 0
            if match:
                number_str = match.group(1)  # group(1) 获取第一个括号中的匹配项
                if '.' in number_str:
                    pf = float(number_str)
                else:
                    pf = int(number_str)

            if pf <= 46 and pf >= 38:

                gs = li.xpath(".//span [@class='jjr_par_dp']/@title")  # 发布房源的经纪公司
                jg = li.xpath(".//b/text()")  # 房租价格
                mz = li.xpath(".//a[@class='strongbox']/text()")  # 发布的标题
                mz1 = ''.join(mz[0].split())  # 保留有效字符
                faBuRiQi = li.xpath(".//div[@class='send-time']/text()")  # 发布时间
                faBuRiQi1 = ''.join(faBuRiQi[0].split())  # 保留有效字符
                tuPanDiZhi = li.xpath(".//img/@lazy_src")
                lianJie = li.xpath(".//div[@class= 'des']/h2/a/@href")
                try:
                    jgint = int(jg[0])
                except ValueError as e:
                    jgint = 10

                # 获取图片名
                tp_ming_zi = ""
                match = re.search(r'/anjuke_58/([0-9a-z]{32})\?', tuPanDiZhi[0])
                if match:
                    tp_ming_zi = match.group(1) + ".png"
                else:
                    tp_ming_zi = self.tp_ming() + ".png"
                # 生成当前时间戳
                current_timestamp = time.time()
                # 先查询数据库有没有同名图片,然对比价格是否一样
                pd = self.sqlm.cha_xun_tp_jg(tp_ming_zi, jgint)
                # 如果都一样
                if pd:
                    # 提取结果中列表pd[2]的最大值
                    max_tuple = max(pd, key=lambda x: x[2])
                    riqi = max_tuple[2]
                    # 24小时 =86400m

                    # 如果数据库的时间距离当前有没有5天
                    if current_timestamp < (riqi + 86400 * 5):
                        continue  # 跳过当前迭代

                # 暂停300毫秒,下载并保存图片
                time.sleep(0.2)
                try:
                    tU_pian = requests.get(tuPanDiZhi[0], headers=headers)
                    tU_pian.raise_for_status()  # 如果响应状态码指示错误，则抛出HTTPError异常
                    tppath = ".\\tp\\" + tp_ming_zi
                    with open(tppath, "wb") as f:
                        f.write(tU_pian.content)
                except RequestException as e:
                    # 处理请求异常
                    print(f"请求图片失败：{e}")
                    raise
                except IOError as e:
                    # 处理文件I/O异常
                    print(f"保存图片失败：{e}")
                    raise
                except Exception as e:
                    # 处理其他可能的异常
                    print(f"发生未知错误：{e}")
                    raise

                print(pf)
                print(jgint)
                print(mz1)
                print(gs[0])
                print(faBuRiQi1)
                print(tuPanDiZhi[0])
                print(lianJie[0])

                # 将数据保存数据库中
                self.sqlm.zj_zu_fang_tiao_mu(tuPanDiZhi[0], tp_ming_zi, pf, gs[0], mz1, jgint, faBuRiQi1, lianJie[0],
                                           current_timestamp)

                print("\r")
                self.cis = self.cis + 1
                # time.sleep(0.3)
        self.__referer = url
        print(self.cis)



if __name__ == '__main__':
    xs = XiaoShuo()
    xs.get_mu_lu('https://sk.3qdu.com/xiaoshuo/629142/')
